package com.example.bookmanagersystem.service;

import com.example.bookmanagersystem.entity.Book;
import com.example.bookmanagersystem.entity.BorrowRecord;
import com.example.bookmanagersystem.entity.Reader;
import com.example.bookmanagersystem.repository.BookRepository;
import com.example.bookmanagersystem.repository.BorrowRecordRepository;
import com.example.bookmanagersystem.repository.ReaderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class BorrowService {
    @Autowired
    private BorrowRecordRepository borrowRecordRepository;

    // 直接依赖BookRepository，而非BookService
    @Autowired
    private BookRepository bookRepository;

    @Autowired
    private ReaderRepository readerRepository;

    // 原有方法保持不变，仅将bookService的调用改为bookRepository
    @Transactional
    public BorrowRecord borrowBook(Long bookId, Long readerId) {
        Optional<Book> bookOptional = bookRepository.findById(bookId);
        Optional<Reader> readerOptional = readerRepository.findById(readerId);

        if (bookOptional.isPresent() && readerOptional.isPresent()) {
            Book book = bookOptional.get();
            Reader reader = readerOptional.get();

            if (book.getStock() <= 0) {
                throw new RuntimeException("图书库存不足");
            }

            // 直接通过bookRepository更新库存（原代码用的是bookService.updateBook）
            book.setStock(book.getStock() - 1);
            bookRepository.save(book); // 直接调用Repository

            BorrowRecord record = new BorrowRecord();
            record.setBook(book);
            record.setReader(reader);
            record.setBorrowDate(LocalDateTime.now());
            return borrowRecordRepository.save(record);
        }

        throw new RuntimeException("图书或读者不存在");
    }

    @Transactional
    public BorrowRecord returnBook(Long recordId) {
        Optional<BorrowRecord> recordOptional = borrowRecordRepository.findById(recordId);

        if (recordOptional.isPresent()) {
            BorrowRecord record = recordOptional.get();

            if (record.getReturnDate() != null) {
                throw new RuntimeException("该图书已经归还");
            }

            record.setReturnDate(LocalDateTime.now());

            // 直接通过bookRepository更新库存（原代码用的是bookService.updateBook）
            Book book = record.getBook();
            book.setStock(book.getStock() + 1);
            bookRepository.save(book); // 直接调用Repository

            return borrowRecordRepository.save(record);
        }

        throw new RuntimeException("借阅记录不存在");

    }

    // 其他方法保持不变...
    public List<BorrowRecord> getAllBorrowRecords() {
        return borrowRecordRepository.findAll();
    }

    public Optional<BorrowRecord> getBorrowRecordById(Long id) {
        return borrowRecordRepository.findById(id);
    }

    public List<BorrowRecord> getUnreturnedRecords() {
        return borrowRecordRepository.findByReturnDateIsNull();
    }

    public List<BorrowRecord> getRecordsByReaderId(Long readerId) {
        return borrowRecordRepository.findByReaderId(readerId);
    }
}